﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace OpenWeb.Dal.Utils
{
    public static class UrlEncoder
    {
        public static string ToFriendlyUrl(this UrlHelper helper,
            string urlToEncode)
        {
            urlToEncode = (urlToEncode ?? "").Trim().ToLower();

            StringBuilder url = new StringBuilder();

            foreach (char ch in urlToEncode)
            {
                switch (ch)
                {
                    case ' ':
                        url.Append('-');
                        break;
                    case '&':
                        url.Append("and");
                        break;
                    case '\'':
                        break;
                    default:
                        if ((ch >= '0' && ch <= '9') ||
                            (ch >= 'a' && ch <= 'z'))
                        {
                            url.Append(ch);
                        }
                        else
                        {
                            url.Append('-');
                        }
                        break;
                }
            }

            return url.ToString();
        }
    }
}


/*
  -> http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls
 
 <a href="<%= Url.Action("View", "Blog", 
   new { id = item.BlogId, blogTitle = Url.ToFriendlyUrl(item.Title) }) %>"> 
   <%= Html.Encode(item.Title) %> 
</a>

public ActionResult Details(int? id, string productTitle) 
{ 
    Product product = ProductRepository.Fetch(id); 
 
    string realTitle = UrlEncoder.ToFriendlyUrl(product.Title); 
    string urlTitle = (productTitle ?? "").Trim().ToLower(); 
 
    if (realTitle != urlTitle) 
    { 
        Response.Status = "301 Moved Permanently"; // Per google indicizzazione
        Response.StatusCode = 301; 
        Response.AddHeader("Location", "/Products/" + product.Id + "/" + realTitle); 
        Response.End(); 
    } 
 
    return View(product); 
}

*/